package com.deyuanyun.pic.ctrl.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.ctrl.controller.vo.RecodeExceptionVO;
import com.deyuanyun.pic.ctrl.controller.vo.query.EqptExceptionVO;
import com.deyuanyun.pic.ctrl.domain.RecodeException;
import com.deyuanyun.pic.ctrl.enumeration.ExceptionHandlingStateEnum;
import com.deyuanyun.pic.ctrl.service.EqptRecodeExceptionService;
import com.deyuanyun.pic.pipe.controller.BaseController;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.service.base.AttachmentService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;

/**
 * 阴保运行管理-异常管理
 * @author lijun
 * @date 2016-09-12
 * 
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/corrctrl/eqptException")
public class EqptRecordExceptionController extends BaseController {

    @Resource
    private EqptRecodeExceptionService eqptRecodeExceptionServiceImpl;
    @Resource
    private AttachmentService attachmentService;

    /**
     * 跳转到异常管理
     */
    @RequestMapping("/recoedException.html")
    public ModelAndView recoedExceptionInit(ModelMap model) {

        model.addAttribute("handlingState", ExceptionHandlingStateEnum.values());

        return forword("/corrctrl/cathodic/exceptionManagement",model);
    }

    /**
     * 查询设备异常信息
     */
    @RequestMapping("/queryRecoedException.json")
    @ResponseBody
    public void queryRecoedException(EqptExceptionVO exception) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        exception.setIsReported(true);
        List<RecodeExceptionVO> recodeExceptionList= eqptRecodeExceptionServiceImpl.queryRecodeException(exception);
        jsonBean.setList(recodeExceptionList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd", jsonBean);
    }

    /**
     * 跳转到异常管理详情页
     */
    @RequestMapping("/recoedExceptionInfo.html")
    public ModelAndView recoedExceptionInfo(String exceptionId, ModelMap model) {
        Asserts.assertTrue(StringUtils.isNotNullEmpty(exceptionId), "没有获取到异常Id");
        RecodeExceptionVO recodeExceptionInfo= eqptRecodeExceptionServiceImpl.queryRecodeExceptionById(exceptionId);
        model.addAttribute("exceptionId",exceptionId);
        model.addAttribute("exceptionInfo",recodeExceptionInfo);

        return forword("/corrctrl/cathodic/exceptionHandling",model);
    }

    /**
     * 查询设备异常详情
     */
    @RequestMapping("/queryRecoedExceptionInfo.json")
    @ResponseBody
    public void queryRecoedExceptionInfo(String exceptionId,ModelMap model) {
        Asserts.assertTrue(StringUtils.isNotNullEmpty(exceptionId), "没有获取到异常Id");
        //异常记录对应的运行记录
        RecodeExceptionVO recodeExceptionInfo= eqptRecodeExceptionServiceImpl.queryRecodeExceptionById(exceptionId);
        List<Attachment> image = new ArrayList<Attachment>();
        if (null != recodeExceptionInfo && null != recodeExceptionInfo.getRecodeId()){
            image = attachmentService.queryAttachment(recodeExceptionInfo.getRecodeId());
        }
        model.addAttribute("imageList", image);
        AjaxSupport.sendSuccessText("success", model);
    }

    /**
     * 导出设备异常信息列表
     */
    @RequestMapping("/queryEqptRecoedExceptionExl.json")
    @ResponseBody
    public void queryEqptDataExl(EqptExceptionVO exception, HttpServletResponse response) throws IOException, ParseException {
        List<String[]> result = eqptRecodeExceptionServiceImpl.queryDataExport(exception);
        String filePath = "/template/excl/ctrl/eqptRecord_exception.xlsx";
        Workbook externalDefect = ExcelUtil.initWorkbook(filePath, result, 1);
        String nowDate = DateUtil.DateToStringYMd(new Date());
        DownExcelUtil.fillWorkbookHead(externalDefect);
        WebUtil.setDownHeader("异常管理导出表"+nowDate +".xlsx");
        externalDefect.write(response.getOutputStream());

    }

    /**
     * 保存设备异常信息列表
     */
    @RequestMapping("/updteEqptRecoedException.json")
    @ResponseBody
    public void updteEqptRecoedException(RecodeException exception) {
        Asserts.assertTrue(StringUtils.isNotNullEmpty(exception.getId()), "没有获取到异常Id");
        int num = eqptRecodeExceptionServiceImpl.updteEqptRecoedException(exception);
        if(num > 0){
            AjaxSupport.sendSuccessText("success", "");
        }else{
            AjaxSupport.sendFailText("fail","");
        }
    }


}
